package model

import (
	"gorm.io/gorm"
	"shopping_07_28/global"
	"time"
)

type Order struct {
	Id          uint32         `gorm:"column:id;type:int UNSIGNED;comment:订单ID;primaryKey;not null;" form:"id" json:"id"`                                          // 订单ID
	UserId      int32          `gorm:"column:user_id;type:int;comment:用户ID;not null;" form:"user_id" json:"user_id"`                                               // 用户ID
	Money       string         `gorm:"column:money;type:varchar(255);comment:金额;not null;" form:"money" json:"money"`                                              // 金额
	Status      int8           `gorm:"column:status;type:tinyint;comment:状态 0:未支付 1:已支付 2:已完成 3:已退款;not null;default:0;" form:"status" json:"status"`              // 状态 0:未支付 1:已支付 2:已完成 3:已退款
	CreatedAt   time.Time      `gorm:"column:created_at;type:datetime(3);comment:下单时间;not null;default:CURRENT_TIMESTAMP(3);" form:"created_at" json:"created_at"` // 下单时间
	UpdatedAt   time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" form:"updated_at" json:"updated_at"`
	ProcessedAt time.Time      `gorm:"column:processed_at;type:datetime(3);comment:处理完成时间;default:NULL;" form:"processed_at" json:"processed_at"` // 处理完成时间
	DeletedAt   gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" form:"deleted_at" json:"deleted_at"`
}

func (o *Order) TableName() string {
	return "order"
}

func (o *Order) AddOrder() error {
	return global.DB.Create(&o).Error
}

func (o *Order) ListOrder() (orders []Order, err error) {
	err = global.DB.Where("status = 2").Find(&orders).Error
	return
}

func (o *Order) ListByErrOrder() (orders []Order, err error) {
	err = global.DB.Where("status = 3").Find(&orders).Error
	return
}

func (o *Order) ListByUserOrder() (orders []Order, err error) {
	err = global.DB.Where("user_id = ?", o.UserId).Find(&orders).Error
	return
}
